package org.csu.laboratoryequipment.controller;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.Equipment;
import org.csu.laboratoryequipment.entity.User;
import org.csu.laboratoryequipment.persistence.EquipmentMapper;
import org.csu.laboratoryequipment.service.EquipmentService;
import org.csu.laboratoryequipment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentMapper equipmentMapper;

    //获取所有equipments
    @GetMapping("equipments")
    @ResponseBody
    public CommonResponse<List<Equipment>> Get(){
        return equipmentService.getEquipmentList();
    }


    //根据id获取某个equipment
    @GetMapping("equipments/{id}")
    @ResponseBody
    public CommonResponse<Equipment> Get(@PathVariable("id") String equipmentid){
        return equipmentService.getEquipmentById(equipmentid);
    }

    //根据id删除某个equipment
    @DeleteMapping("equipments/{id}")
    @ResponseBody
    public CommonResponse<List<Equipment>> DELETE(@PathVariable("id") String id){
        return equipmentService.deleteEquipmentById(id);
    }

    //添加一个equipment
    @PostMapping("equipments")
    @ResponseBody
    public CommonResponse<List<Equipment>> POST(
            @RequestParam("equipmentId") String equipmentId,
            @RequestParam("name") String name,
            @RequestParam("quantity") int quantity,
            @RequestParam("status") String status,
            @RequestParam("specifications") String specifications,
            @RequestParam("purchaseDate") String purchaseDate,
            @RequestParam("purchasePrice") double purchasePrice,
            @RequestParam("remarks") String remarks
    ) throws ParseException {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date purchaseDate1 = dateFormat.parse(purchaseDate);
        return equipmentService.postEquipment(equipmentId,name,quantity,status,specifications,purchaseDate1,purchasePrice,remarks);
    }


    //修改
    @PutMapping("equipments")
    @ResponseBody
    public CommonResponse<List<Equipment>> PUT(
            @RequestParam("equipmentId") String equipmentId,
            @RequestParam("name") String name,
            @RequestParam("quantity") int quantity,
            @RequestParam("status") String status,
            @RequestParam("specifications") String specifications,
            @RequestParam("purchaseDate") String purchaseDate,
            @RequestParam("purchasePrice") double purchasePrice,
            @RequestParam("remarks") String remarks
    ) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date purchaseDate1 = dateFormat.parse(purchaseDate);
        return equipmentService.putEquipment(equipmentId,name,quantity,status,specifications,purchaseDate1,purchasePrice,remarks);
    }
}
